﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class CountriesDetail : Form
    {
        private Countries _parent;
        public DataSets.Dictionary.CountriesDataTable CountriesData
        {
            get
            {
                return _parent.dictionary.Countries;                
            }
        }
        private Util.ActionType _actType;

        public Util.ActionType ActionType
        {
            get { return _actType; }
            set { _actType = value; }
        }
        private Int32 _countryId;

        public Int32 CountyId
        {
            get { return _countryId; }
            set { _countryId = value; }
        }
        private DataSets.Dictionary.CountriesRow _curRow;

        public DataSets.Dictionary.CountriesRow CurrentRow
        {
            get { return _curRow; }
            set { _curRow = value; }
        }

        
        public CountriesDetail()
        {
            InitializeComponent();
            
        }
        public CountriesDetail(DIProject.Util.ActionType actType, Int32 countryId,Countries parent)
        {
            InitializeComponent();
            _parent = parent;
            ActionType = actType;
            CountyId = countryId;
        }

        private void CountriesDetail_Load(object sender, EventArgs e)
        {
            switch (this.ActionType)
            {
                case DIProject.Util.ActionType.Insert:
                    InitInsert();
                    break;
                case DIProject.Util.ActionType.Update:
                    InitUpdate();
                    break;
                default:
                    this.Close();
                    break;
            }
        }
        private void InitInsert()
        {
            btnDelete.Visible = false;
            btnSaveUpdate.Text = "Сохранить";
        }
        private void InitUpdate()
        {
            btnDelete.Visible = true;
            btnSaveUpdate.Text = "Изменить";
            this.CurrentRow = CountriesData.FindById(this.CountyId);
            txbName.Text = this.CurrentRow.Name;
            txbShortName.Text = this.CurrentRow.ShortName;
            txbNote.Text = this.CurrentRow.Note;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Вы действительно хотите удалить страну " + CurrentRow.Name + "?", "Удаление страны", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                DIProject.DataSets.DictionaryTableAdapters.CountriesTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.CountriesTableAdapter();
                CurrentRow.Delete();
                if (DIProject.Data.Sync.IsAdmin)
                {
                    CurrentRow.MW = Properties.Resources.strPhysicallyDelete;
                }
                adapter.Update(CountriesData);
                CountriesData.AcceptChanges();
            }
            this.Close();
        }

        private void btnSaveUpdate_Click(object sender, EventArgs e)
        {
            DIProject.DataSets.DictionaryTableAdapters.CountriesTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.CountriesTableAdapter();
            if (this.ActionType == DIProject.Util.ActionType.Update)
            {
                CurrentRow.Name = txbName.Text;
                CurrentRow.ShortName = txbShortName.Text;
                CurrentRow.Note = txbNote.Text;
                CurrentRow.MD = DateTime.Now;
                string MW = FirstForm.Login;
                if (string.IsNullOrEmpty(MW))
                    MW = string.Empty;
                CurrentRow.MW = MW;
                adapter.Update(CountriesData);
                CurrentRow.Original_ID = CurrentRow.Id;
                CurrentRow.Original_MD = CurrentRow.MD;
                CountriesData.AcceptChanges();
            }
            else
                if (this.ActionType == DIProject.Util.ActionType.Insert)
                {
                    DataSets.Dictionary.CountriesRow cr = CountriesData.NewCountriesRow();
                    cr.Name = txbName.Text;
                    cr.Note = txbNote.Text;
                    cr.ShortName = txbShortName.Text;
                    cr.MD = DateTime.Now;
                    string MW = FirstForm.Login;
                    if(string.IsNullOrEmpty(MW))
                        MW = string.Empty;
                    cr.MW = MW;
                    CountriesData.Rows.Add(cr);
                    adapter.Update(CountriesData);
                    cr.Original_ID = cr.Id;
                    cr.Original_MD = cr.MD;
                    CountriesData.AcceptChanges();
                }
                else
                {
                    this.Close();
                    return;
                }
            this.Close();
        }
    }
}